package com.sss.hloster0304;

public  class SepStack implements Stack {
	final char defaultSize = 10;
	int top;// 标记栈内元素个数，即栈顶元素
	Object[] stack;
	int maxStackSize;

	public SepStack() {
		initiate(defaultSize);
	}

	public SepStack(int sz) {
		initiate(sz);
	}

	private void initiate(int sz) {
		maxStackSize = sz;
		top = 0;
		stack = new Object[sz];
	}

//实现把数据元素压入栈--进栈
	public void push(Object obj) throws Exception {
		if (top == maxStackSize) {
			throw new Exception("堆栈已满！");
		}
//进栈，栈顶标记+1
		stack[top] = obj;
		top++;
	}

//实现获取并删除栈顶数据元素--退栈

	public Object pop() throws Exception {
		if (top == 0) {
			throw new Exception("堆栈已空！");
		}
		// 返回退栈数据元素，栈顶标记减1实现删除（实际并未删除）
		top--;
		return stack[top];

	}

//实现获取但不删除栈顶数据元素
	public Object getTop() throws Exception {
		if (top == 0) {
			throw new Exception("堆栈已空");
		}

		return stack[top - 1];
	}

//实现判断栈是否为空
	public boolean notEmpty() {
		return (top > 0);
	}

	

}
